package cn.swenty.backstage.framework.taglib;

import java.util.List;

import cn.swenty.backstage.framework.common.tree.TreeNode;
import cn.swenty.backstage.framework.common.util.StringUtil;

public class TreeTag extends SimpleTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String node;
	private String nodeAction;
	private Boolean openAll = false;
	private Boolean check = false;
	private String selectValues;
	private Boolean lazyLoad;
	private String width;
	private String checks;
	private String modify;
	private String drop;
	private String icons;
	private String hideNodes;
	private String hideChekLevel;
	private String hideCheks;
	private String openLevel;
	private String checkAction;
	private String chkboxType = "{ \"Y\" : \"p\", \"N\" : \"s\" }";
	
	
	public String getHideCheks() {
		return hideCheks;
	}

	public void setHideCheks(String hideCheks) {
		this.hideCheks = hideCheks;
	}

	public String getChkboxType() {
		return chkboxType;
	}

	public void setChkboxType(String chkboxType) {
		this.chkboxType = chkboxType;
	}

	public String getCheckAction() {
		return checkAction;
	}

	public void setCheckAction(String checkAction) {
		this.checkAction = checkAction;
	}

	public String getOpenLevel() {
		return openLevel;
	}

	public void setOpenLevel(String openLevel) {
		this.openLevel = openLevel;
	}

	/**只有在openAll等于false时起作用**/
	private String openNodes;
	
	
	public String getHideChekLevel() {
		return hideChekLevel;
	}

	public void setHideChekLevel(String hideChekLevel) {
		this.hideChekLevel = hideChekLevel;
	}

	/* (non-Javadoc)
	 * @see cn.yxcps.crm.framework.taglib.SimpleTag#getPrintStr()
	 */
	protected String getPrintStr() throws Exception {

		StringBuffer sb = new StringBuffer();
		
		this.addLine(sb, "<link href=\"" + this.webRoot()	+ "/asserts/js/zTree/css/zTreeStyle/zTreeStyle.css\" rel=\"stylesheet\" type=\"text/css\" />");
		this.addLine(sb, "<script language='Javascript' src='" + this.webRoot()	+ "/asserts/js/zTree/js/jquery.ztree.core-3.5.min.js'></script>");
		this.addLine(sb, "<script language='Javascript' src='" + this.webRoot()	+ "/asserts/js/zTree/js/jquery.ztree.excheck-3.5.min.js'></script>");
		this.addLine(sb, "<script language='javascript'>");
		this.addLine(sb, " function zTreeOnClick(event, treeId,treeNode){");
		if(StringUtil.isNotNullTrim(nodeAction)){
			this.addLine(sb, " eval('"+nodeAction+"(treeNode)')");
		}
		this.addLine(sb, " }");
		
		this.addLine(sb, " function zTreeOnCheck(event, treeId,treeNode){");
		if(StringUtil.isNotNullTrim(checkAction) && check){
			this.addLine(sb, " eval('"+checkAction+"(treeNode)')");
		}
		this.addLine(sb, " }");
		
		
		
		this.addLine(sb, " var  setting={isSimpleData : true,data:{key:{name:'name'},simpleData :{enable:true,idKey : 'id',pIdKey : 'pid',rootPId : '0'}},check: {enable:"+this.check+",nocheckInherit: false,"+(StringUtil.isNotNull(chkboxType)?("chkboxType:"+chkboxType):"chkboxType :{ \"Y\" : \"p\", \"N\" : \"s\" }")+"},callback:{onClick:zTreeOnClick,onCheck:zTreeOnCheck}}; var data = []; var tree;");
		// 如果有回调
		if (StringUtil.isNotNullTrim(selectValues)) {
			this.addLine(sb, " function "+selectValues+"(){var nodes = tree.getCheckedNodes(true); return nodes;}");
		}
/*		if(StringUtil.isNotNull(modify)){
		this.addLine(sb, " function "+modify+"(node){");
		this.addLine(sb, " if(node){var ztree =  $.fn.zTree.getZTreeObj('"+id+"'); var temp_node = ztree.getNodeByTId(node.id); if(temp_node){temp_node.editName(treeNode);} }");
		this.addLine(sb, " }");
		}
*/		List<TreeNode> nodes = (List<TreeNode>) this.getPageValue(pageContext, node);
		String jsData = "";
		if(nodes!=null){
			for(TreeNode tn:nodes){
				jsData += addNode(tn, "",0);
			}
		}
		this.addLine(sb,jsData);
		this.addLine(sb,
				"$(function(){tree =  $.fn.zTree.init($(\"#"+id+"\"), setting, data);"
						+(openAll?"tree.expandAll(true)":""));
		if(check&&StringUtil.isNotNullTrim(checks)){
			String [] cs = checks.split(",");
			for(int i=0;i<cs.length;i++){
				this.addLine(sb,"var _node_check = (tree.getNodeByParam(\"id\", \""+cs[i]+"\", null)); if(_node_check){tree.checkNode(tree.getNodeByParam(\"id\", \""+cs[i]+"\", null), true, true);}");
			}
		}
		if(StringUtil.isNotNull(hideNodes)){
			String[]hns = hideNodes.split(",");
			for(int i=0;i<hns.length;i++){
				this.addLine(sb,"var _node_check =(tree.getNodeByParam('id','"+hns[i]+"'));if(_node_check){tree.removeNode(_node_check)}");
			}
		}
		
		if(StringUtil.isNotNull(hideCheks)){
			String[]hns = hideCheks.split(",");
			for(int i=0;i<hns.length;i++){
				this.addLine(sb,"var _node_check =(tree.getNodeByParam('id','"+hns[i]+"'));");
				this.addLine(sb,"if(_node_check){tree.setChkDisabled(_node_check,true);}");
			}
		}
		
		
		//如果有隐藏的级别标识 且check为true
		if(StringUtil.isNotNull(hideChekLevel) && check){
			String [] hcls = hideChekLevel.split(",");
			this.addLine(sb, "var nodes=tree.getNodes(); var arr1 = findDown(nodes,[]);  ");
			for(String hcl:hcls){
				int h = Integer.parseInt(hcl);
				this.addLine(sb, " for(var m=0;m<arr1.length;m++) {");
					
				this.addLine(sb, "  if(arr1[m].level=="+h+"){");
				this.addLine(sb, " arr1[m].nocheck = true;tree.updateNode(arr1[m]); ");
				this.addLine(sb, " }");
				this.addLine(sb, " }");
				
			}
		}
		
		//如果有隐藏的级别标识 且check为true
		if(StringUtil.isNotNull(openLevel)){
			String [] hcls = openLevel.split(",");
			this.addLine(sb, "var nodes=tree.getNodes(); var arr1 = findDown(nodes,[]);  ");
			for(String hcl:hcls){
				int h = Integer.parseInt(hcl);
				this.addLine(sb, " for(var m=0;m<arr1.length;m++) {");
					
				this.addLine(sb, "  if(arr1[m].level=="+h+"){");
				this.addLine(sb, "  tree.expandNode(arr1[m],true,false)");
				this.addLine(sb, " }");
				this.addLine(sb, " }");
				
			}
		}
	
		this.addLine(sb,"});");
		this.addLine(sb, " function findDown(nodes,arr){ if(!arr){arr = []} for(var i=0;i<nodes.length;i++){ if(nodes[i].children){arr.push(findDown(nodes[i].children,arr));} arr.push(nodes[i]); }  return arr;}");
		this.addLine(sb, "</script>");
		this.addLine(sb,	"<ul id=\""+id+"\" class=\"ztree\" style=\"overflow:auto;\"></ul>");
		return sb.toString();
	}

	public String addNode(TreeNode node,String js,int level){
		boolean isOpen = false;
		if(StringUtil.isNotNull(openNodes)){
			String ons[] = openNodes.split(",");
			for(int i=0;i<ons.length;i++){
				if(ons[i].equals(node.getId())){
					isOpen = true;
				}
			}
		}
		if(node.getId()!=null){
			if(isOpen){
				js +=" data.push({id:'"+(node.getId().replace("'", "\\'"))+"',name:'"+(node.getName().replace("'", "\\'"))+"',pid:'"+(StringUtil.isNotNullTrim(node.getParentId())?(node.getParentId().replace("'", "\\'")):"0")+"',open:true});";
			}else{
				js +=" data.push({id:'"+(node.getId().replace("'", "\\'"))+"',name:'"+(node.getName().replace("'", "\\'"))+"',pid:'"+(StringUtil.isNotNullTrim(node.getParentId())?(node.getParentId().replace("'", "\\'")):"0")+"'});";
			}
		}
	
		if(node.getChildren()!=null&&node.getChildren().size()>0){
			for(TreeNode child:(List<TreeNode>)node.getChildren()){
				if(child.getId()!=null && child.getName()!=null){
					js+=addNode(child, "",level+1);
				}
			}
		}
		return js;
	}
	
	public String getModify() {
		return modify;
	}

	public void setModify(String modify) {
		this.modify = modify;
	}

	public String getDrop() {
		return drop;
	}

	public void setDrop(String drop) {
		this.drop = drop;
	}

	public String getChecks() {
		return checks;
	}

	public void setChecks(String checks) {
		this.checks = checks;
	}

	public String getHideNodes() {
		return hideNodes;
	}

	public void setHideNodes(String hideNodes) {
		this.hideNodes = hideNodes;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public String getNodeAction() {
		return nodeAction;
	}

	public void setNodeAction(String nodeAction) {
		this.nodeAction = nodeAction;
	}

	public Boolean isOpenAll() {
		return openAll;
	}

	public void setOpenAll(Boolean openAll) {
		this.openAll = openAll;
	}

	public String getSelectValues() {
		return selectValues;
	}

	public void setSelectValues(String selectValues) {
		this.selectValues = selectValues;
	}

	public Boolean isLazyLoad() {
		return lazyLoad;
	}

	public Boolean isCheck() {
		return check;
	}

	public void setCheck(Boolean isCheck) {
		this.check = isCheck;
	}

	public void setLazyLoad(Boolean lazyLoad) {
		this.lazyLoad = lazyLoad;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getOpenNodes() {
		return openNodes;
	}

	public void setOpenNodes(String openNodes) {
		this.openNodes = openNodes;
	}

	public String getIcons() {
		return icons;
	}

	public void setIcons(String icons) {
		this.icons = icons;
	}
	
}
